package com.onedriver.doc4j;

import org.docx4j.convert.in.xhtml.XHTMLImporter;
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import java.io.*;

public class HtmlToWordConverter2 {
    public static void convertHtmlToWord(String html, String outputPath) throws Docx4JException {

            // 创建一个新的Word文档
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

            // 将HTML内容导入文档
            XHTMLImporter importer = new XHTMLImporterImpl(wordMLPackage);


            wordMLPackage.getMainDocumentPart().getContent().addAll(importer.convert(html, null));

            // 保存文档
            wordMLPackage.save(new File(outputPath));

            System.out.println("HTML转Word成功！");

    }

    public static void main(String[] args) throws Docx4JException {
        String html = "<html><body><p>Hello, World!</p></body></html>";
        String outputPath = "output.docx";
        convertHtmlToWord(html, outputPath);
    }
}